home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / test / oio.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  7KB  |  248 lines

  1. /* Test Object I/O
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet:kgorlen@alw.nih.gov
  20.  
  21. Function:
  22.     
  23. Modification History:
  24.     
  25. $Log:    oio.c,v $
  26.  * Revision 3.0  90/05/20  00:29:29  kgorlen
  27.  * Release for 1st edition.
  28.  * 
  29. */
  30. static char rcsid[] = "$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/test/RCS/oio.c,v 3.0 90/05/20 00:29:29 kgorlen Rel $";
  31.  
  32. #include <osfcn.h>
  33. #include "nihclconfig.h"
  34. #include "ArrayOb.h"
  35. #include "Arraychar.h"
  36. #include "Assoc.h"
  37. #include "Bag.h"
  38. #include "Bitset.h"
  39. #include "Date.h"
  40. #include "Dictionary.h"
  41. #include "FDSet.h"
  42. #include "Float.h"
  43. #include "Fraction.h"
  44. #include "Heap.h"
  45. #include "LinkedList.h"
  46. #include "LinkOb.h"
  47. #include "OrderedCltn.h"
  48. #include "Point.h"
  49. #include "Range.h"
  50. #include "Rectangle.h"
  51. #include "Set.h"
  52. #include "SortedCltn.h"
  53. #include "Stack.h"
  54. #include "String.h"
  55. #include "Time.h"
  56. #include "OIOnih.h"
  57. #include "OIOfd.h"
  58.  
  59. Object* testStore()
  60. {
  61.     ArrayOb& arrayob = *new ArrayOb(4);
  62.     Arraychar& arraychar = *new Arraychar(26);
  63.     Bag& bag = *new Bag(100);
  64.     Bitset& bitset = *new Bitset(4,6);
  65.     Date& date = *new Date(11,"Nov",48);
  66.     Dictionary& dict = *new Dictionary(100);
  67.     FDSet& fdset = *new FDSet(); fdset[0] = fdset[1] = fdset[19] = 1;
  68.     Float& flt = *new Float(3.1415);
  69.     Fraction& fract = *new Fraction(22,7);
  70.     Heap& heap = *new Heap;
  71.     LinkedList& linkedlist = *new LinkedList;
  72.     OrderedCltn& orderedcltn = *new OrderedCltn;
  73.     Point& point = *new Point(1,2);
  74.     Range& range = *new Range(-1,1);
  75.     Rectangle& rect = *new Rectangle(0,1,2,3);
  76.     Set& set = *new Set(100);
  77.     SortedCltn& sortedcltn = *new SortedCltn;
  78.     Stack& stack = *new Stack;
  79.     String& str = *new String("123456789a123456789b123456789c123456789d123456789e123456789f123456789g12345678\012h Hello, world!\t{\033\"\\}");
  80.     Time& time = *new Time(date,1,2,3);
  81.     
  82.     arrayob[0] = &bitset; arrayob[1] = &point; arrayob[2] = &str;
  83.     for (int i = 0; i < 26; i++) arraychar[i] = "abcdefghijklmnopqrstuvwxyz"[i];
  84.     orderedcltn.add(point); orderedcltn.add(rect); orderedcltn.add(str);
  85.     orderedcltn.add(flt); orderedcltn.add(fract); orderedcltn.add(range);
  86.     orderedcltn.add(date); orderedcltn.add(time); orderedcltn.add(fdset);
  87.     stack.push(bitset); stack.push(orderedcltn);
  88.     bag.addWithOccurrences(point,1);
  89.     bag.addWithOccurrences(rect,2);
  90.     bag.addWithOccurrences(str,3);
  91.     bag.add(orderedcltn);
  92.     sortedcltn.add(*new String("Huey"));
  93.     sortedcltn.add(*new String("Dewey"));
  94.     sortedcltn.add(*new String("Louie"));
  95.     heap.add(*new String("Dasher"));
  96.     heap.add(*new String("Dancer"));
  97.     heap.add(*new String("Prancer"));
  98.     heap.add(*new String("Vixen"));
  99.     heap.add(*new String("Comet"));
  100.     heap.add(*new String("Cupid"));
  101.     heap.add(*new String("Donder"));
  102.     heap.add(*new String("Blitzen"));
  103.     dict.add(*new Assoc(orderedcltn,point));
  104.     dict.add(*new Assoc(bag,rect));
  105.     dict.add(*new Assoc(sortedcltn,stack));
  106.     linkedlist.add(*new LinkOb(point));
  107.     linkedlist.add(*new LinkOb(rect));
  108.     linkedlist.add(*new LinkOb(bitset));
  109.     linkedlist.add(*new LinkOb(str));
  110.     
  111.     set.add(arrayob);
  112.     set.add(arraychar);
  113.     set.add(bag);
  114.     set.add(dict);
  115.     set.add(heap);
  116.     set.add(linkedlist);
  117.     set.add(orderedcltn);
  118.     set.add(sortedcltn);
  119.     set.add(stack);
  120.     
  121.     OrderedCltn& monster = *new OrderedCltn;
  122.     monster.add(set); monster.add(*set.deepCopy());
  123.     return &monster;
  124. }
  125.  
  126. #include <fcntl.h>
  127. #include <osfcn.h>
  128.  
  129. #ifndef BSD
  130.  
  131. const char* optarg;
  132. int optind = 1;
  133. static char* optchar = "?";
  134.  
  135. int getopt(int /*argc*/, const char** argv, const char* optstr) {
  136.   int optlen = strlen(optstr);
  137.   int inx;
  138.  
  139.   if ( argv[optind]==0 || strlen(argv[optind]) < 2 || *argv[optind] != '-' ) { /* not a switch */
  140.      return EOF; 
  141.      };
  142.    *optchar = argv[optind][1];
  143.      
  144. //  inx = strcspn(optstr,optchar);
  145.   for(inx=0; inx<= strlen(optstr); inx++)
  146.     if ( optstr[inx] == *optchar ) break;
  147.      
  148.   if ( inx == optlen ) {            /* illegal switch */
  149.     ++optind;
  150.     fprintf(stderr,"getopt> illegal switch\n");
  151.     return '?';
  152.     };
  153.  
  154.   if ( (inx+1 <= optlen-1) && (optstr[inx+1] == ':') ) {/*switch has argument*/
  155.     if (strlen(argv[optind]) == 2)
  156.       optarg = argv[++optind];           /* optarg is next argv */
  157.     else 
  158.       optarg = &argv[optind][2];           /* optarg in this argv */
  159.     };
  160.   ++optind;
  161.     return *optchar;
  162. }
  163.  
  164. #endif
  165.  
  166. main(int argc, char** argv)
  167. {
  168.     extern int optind;        // used by getopt()
  169.     extern char* optarg;        // used by getopt()
  170.     char* filea = "oio.obj";
  171.     char* fileb;
  172.     bool binaryIO = NO;    // test binary storeOn/readFrom flag
  173.     bool dumpOpt = NO;    // dump read object on cout
  174.     bool filebOpt = NO;    // file to store object read specified
  175.  
  176. // parse command options    
  177.  
  178.     int c;
  179.     while ((c = getopt (argc, argv, "bd")) != EOF)
  180.         switch    (c) {
  181.             case 'b': binaryIO = YES; break;
  182.             case 'd': dumpOpt = YES; break;
  183.             case '?':
  184.                 cerr << "usage: [ -bd ] [ filea [ fileb ] ]\n ";
  185.                 exit(2);
  186.         }
  187.     if (optind < argc) filea = argv[optind++];
  188.     if (optind < argc) {
  189.         fileb = argv[optind++];
  190.         filebOpt = YES;
  191.     }
  192.                 
  193.     Object* a = testStore();
  194.     Object* b;
  195.     
  196.     if (binaryIO) {
  197.         cout << "Test storeOn(OIOofd&)" << endl;
  198.         int fd = creat(filea,0664);
  199.         a->storeOn(OIOofd(fd));
  200.         close(fd);
  201.         cout << "Test readFrom(OIOifd&)" << endl;
  202.         fd = open(filea,O_RDONLY);
  203.         b = Object::readFrom(OIOifd(fd));
  204.         close(fd);
  205.     }
  206.     else {
  207.         cout << "Test storeOn(OIOout&)" << endl;
  208.         ostream* out = new ostream(creat(filea,0664));
  209.         a->storeOn(OIOnihout(*out));
  210.         delete out;
  211.         cout << "Test readFrom(OIOin&)" << endl;
  212.         istream* in = new istream(open(filea,O_RDONLY));
  213.         b = Object::readFrom(OIOnihin(*in));
  214.         delete in;
  215.     }
  216.  
  217.     
  218. // compare object stored with object read    
  219.     
  220.     if (a->isEqual(*b)) {
  221.         cout << "SUCCESS! -- stored object equals object read" << endl;
  222.         if (dumpOpt) {
  223.             b->dumpOn(cout);
  224.             cout << endl;
  225.         }
  226.     }
  227.     else {
  228.         cout << "FAILURE! -- stored object does not equal object read" << endl;
  229.         cout << "*** object stored:" << endl;  a->dumpOn(cout);  cout << endl;
  230.         cout << "*** object read:" << endl;  b->dumpOn(cout);  cout << endl;
  231.     }
  232.     
  233. // store object read    
  234.  
  235.     if (filebOpt) {
  236.         if (binaryIO) {
  237.             int fd =creat(fileb,0664);
  238.             b->storeOn(OIOofd(fd));
  239.             close(fd);
  240.         }
  241.         else {
  242.             ostream out(creat(fileb,0664));
  243.             b->storeOn(OIOnihout(out));
  244.         }
  245.     }
  246. }
  247.  
  248.